package com.lib.common.helper.api

import com.lib.common.constant.RouterPathConstant
import com.lib.common.helper.arouter.ARouterHelper
import com.lib.common.repository.datacenter.DataCenter
import com.lib.common.utils.ComUtils
import com.lib.framework.manager.ContextManager
import com.lib.framework.network.adapter.request.ApiRequestCallback
import com.lib.framework.widget.WidgetUtil

/**
 * Copyright (C), 2019-2020
 * FileName: ComRequestCallback
 * Author: Kevin
 * Date: 2020-01-07 16:09
 * Description: 公共的网络请求回调
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
abstract class ComRequestCallback<T> : ApiRequestCallback<T> {

    /**
     * 拦截成功时的结果
     */
    protected open fun interceptSuccess(data: T?): Boolean {
        if (data is ApiResponse<*>) {
            if (data.error?.code == "4002") {
                if(DataCenter.isLogin()) {
                    ComUtils.logout()
                    // 跳到登陆页面
//                    ARouterHelper.buildPostcard(RouterPathConstant.PATH_PHONE_LOGIN)
//                        .navigation()
                }
                return false
            }
        }

        return false
    }

    override fun onSuccess(data: T?) {
        if (interceptSuccess(data)) return

        onSuccess02(data)
    }

    abstract fun onSuccess02(data: T?)

    override fun onFailure(t: Throwable) {
        WidgetUtil.showPromptDialog(ContextManager.applicationContext,"网络连接异常")
    }
}